<?php

use common\models\c2\statics\AttributeInputType;
use yii\helpers\Html;
use kartik\widgets\ActiveForm;
use kartik\builder\Form;
use kzo\base\models\statics\EntityModelStatus;
use kzo\base\widgets\ui\bs4\Alert;
use yii\helpers\Url;

/* @var $model common\models\c2\entity\Attribute */

$regularLangName = \Yii::$app->kzoHelper->getRegularLangName();
$messageName = $model->getMessageName();
?>

<?php
$form = ActiveForm::begin([
    'action' => ['edit', 'id' => $model->id],
    'options' => [
        'id' => $model->getBaseFormName(),
        'data-pjax' => true,
    ]]);
?>

    <div class="<?= $model->getPrefixName('form') ?> card bg-light mt-2">
        <div class="card-body">

            <?php if (Yii::$app->session->hasFlash($messageName)): ?>
                <?php if (!$model->hasErrors()) {
                    echo Alert::widget([
                        'type' => Alert::TYPE_SUCCESS,
                        'title' => 'Well Down',
                        'icon' => 'fas fa-ok-circle',
                        'body' => Yii::$app->session->getFlash($messageName),
                        'showSeparator' => true,
                        'delay' => 2000
                    ]);
                } else {
                    echo Alert::widget([
                        'type' => Alert::TYPE_WARNING,
                        'title' => 'Warning!',
                        'icon' => 'fas fa-ok-circle',
                        'body' => Yii::$app->session->getFlash($messageName),
                        'showSeparator' => true,
                    ]);
                }
                ?>
            <?php endif; ?>

            <?php
            echo Form::widget([
                'model' => $model,
                'form' => $form,
                'columns' => 4,
                'attributes' => [
                    'type' => ['type' => Form::INPUT_DROPDOWN_LIST, 'items' => []],
                    'code' => ['type' => Form::INPUT_TEXT, 'options' => ['placeholder' => $model->getAttributeLabel('code')]],
                    'name' => ['type' => Form::INPUT_TEXT, 'options' => ['placeholder' => $model->getAttributeLabel('name')]],
                    'label' => ['type' => Form::INPUT_TEXT, 'options' => ['placeholder' => $model->getAttributeLabel('label')]],
                    'hint' => ['type' => Form::INPUT_TEXT, 'options' => ['placeholder' => $model->getAttributeLabel('hint')]],
                    'input_type' => ['type' => Form::INPUT_DROPDOWN_LIST,
                        'items' => \common\models\c2\statics\AttributeInputType::getHashMap('id', 'label'),
                        'options' => ['placeholder' => $model->getAttributeLabel('input_type')]],
                    'default_value' => ['type' => Form::INPUT_TEXTAREA,
                        'widgetClass' => '\vova07\imperavi\Widget',
                    ],
                ]]);

            echo Html::beginTag('div', ['class' => 'card mb-2', 'id' => 'items-card']);
            echo Html::beginTag('div', ['class' => 'card-body']);

            echo Form::widget([
                'model' => $model,
                'form' => $form,
                'columns' => 1,
                'attributes' => [
                    'items' => [
                        'type' => Form::INPUT_WIDGET,
                        'widgetClass' => unclead\multipleinput\MultipleInput::className(),
                        'options' => [
                            'addButtonOptions' => [
                                'class' => 'btn btn-success',
                                'label' => Html::tag('i', null, ['class' => 'fa fa-plus'])
                            ],
                            'removeButtonOptions' => [
                                'class' => 'btn btn-danger',
                                'label' => Html::tag('i', null, ['class' => 'fa fa-minus'])
                            ],
                            'data' => $model->items,
                            // 'max' => 4,
                            'allowEmptyList' => true,
                            'rowOptions' => function ($model, $index, $context) {
                                return ['data-id' => $model['id']];
                            },
                            'columns' => [
                                [
                                    'name' => 'id',
                                    'type' => 'hiddenInput',
                                ],
                                // [
                                //     'name' => 'code',
                                //     'title' => Yii::t('app.c2', 'Code'),
                                //     'enableError' => true,
                                //     'options' => [
                                //         'class' => 'input-priority'
                                //     ]
                                // ],
                                [
                                    'name' => 'label',
                                    'title' => Yii::t('app.c2', 'Label'),
                                    'enableError' => true,
                                    'options' => [
                                        'class' => 'input-priority'
                                    ]
                                ],
                                [
                                    'name' => 'value',
                                    'title' => Yii::t('app.c2', 'Value'),
                                    'enableError' => true,
                                    'options' => [
                                        'class' => 'input-priority'
                                    ]
                                ],
                                [
                                    'name' => 'is_selected',
                                    'type' => 'dropDownList',
                                    'title' => Yii::t('app.c2', 'Is Selected'),
                                    'defaultValue' => 2,
                                    'items' => \common\models\c2\statics\WhetherType::getHashMap('id', 'label'),
                                ],
                                [
                                    'name' => 'position',
                                    'type' => kartik\widgets\TouchSpin::className(),
                                    'title' => Yii::t('app.c2', 'Position'),
                                    'defaultValue' => 50,
                                ],
                            ]
                        ],
                    ],
                ]
            ]);

            echo Html::endTag('div');
            echo Html::endTag('div');

            echo Form::widget([
                'model' => $model,
                'form' => $form,
                'columns' => 4,
                'attributes' => [
                    'is_sku' => [
                        'type' => Form::INPUT_WIDGET,
                        'hint' => Yii::t('app.c2', "Tips: if checked, will be act as product sku attribute."),
                        'widgetClass' => '\kartik\checkbox\CheckboxX',
                        'options' => [
                            'pluginOptions' => ['threeState' => false],
                        ],
                    ],
                    'is_visible' => [
                        'type' => Form::INPUT_WIDGET,
                        'widgetClass' => '\kartik\checkbox\CheckboxX',
                        'options' => [
                            'pluginOptions' => ['threeState' => false],
                        ],
                    ],
                    'is_required' => [
                        'type' => Form::INPUT_WIDGET, 'hint' => Yii::t('app.c2', "Tips: if checked, will be act as required attribute."),
                        'widgetClass' => '\kartik\checkbox\CheckboxX', 'options' => [
                            'pluginOptions' => ['threeState' => false],
                        ],],
                    'is_unique' => [
                        'type' => Form::INPUT_WIDGET, 'hint' => Yii::t('app.c2', "Tips: if checked, will be validate unique when input."),
                        'widgetClass' => '\kartik\checkbox\CheckboxX', 'options' => [
                            'pluginOptions' => ['threeState' => false],
                        ],],
                    'is_depend' => [
                        'type' => Form::INPUT_WIDGET,
                        'hint' => Yii::t('app.c2', "Tips: if checked, will be validate unique when input."),
                        'widgetClass' => '\kartik\checkbox\CheckboxX',
                        'options' => [
                            'pluginOptions' => ['threeState' => false],
                        ],
                    ],
                    'depend_id' => [
                        'type' => Form::INPUT_WIDGET,
                        'widgetClass' => '\kartik\widgets\Select2',
                        'options' => [
                            'language' => Yii::$app->language,
                            'data' => $model::getHashMap('id', 'label'),
                            'pluginOptions' => [
                                'allowClear' => false,
                                'placeholder' => Yii::t('app.c2', "Select options .."),
                            ],
                        ],
                    ],

                ]
            ]);

            echo Form::widget([
                'model' => $model,
                'form' => $form,
                'columns' => 4,
                'attributes' => [
                    'memo' => ['type' => Form::INPUT_TEXTAREA,
                    ],
                    'status' => ['type' => Form::INPUT_DROPDOWN_LIST, 'items' => EntityModelStatus::getHashMap('id', 'label')],
                    'position' => ['type' => Form::INPUT_WIDGET, 'widgetClass' => '\kartik\touchspin\TouchSpin',],
                ]
            ]);

            echo Html::submitButton('<i class="fa fa-save"></i> ' . Yii::t('app.c2', 'Save'), ['type' => 'button', 'class' => 'btn btn-primary float-right ml-2']);
            echo Html::a('<i class="fa fa-arrow-left"></i> ' . Yii::t('app.c2', 'Go Back'), ['index'], ['data-pjax' => '0', 'class' => 'btn btn-light float-right', 'title' => Yii::t('app.c2', 'Go Back'),]);
            ?>

        </div>
    </div>
<?php ActiveForm::end(); ?>

<?php
$js = "";
$js .= "var inputType = '#" . Html::getInputId($model, 'input_type') . "'\n";
$js .= "var item = '#" . Html::getInputId($model, 'item') . "'\n";
$js .= "var isDepend = '#" . Html::getInputId($model, 'is_depend') . "'\n";
$js .= "var dependId = '#" . Html::getInputId($model, 'depend_id') . "'\n";

// handle items input

if (!$model->isNewRecord && !$model->isMultiple()) {
    $js .= "$('#items-card').hide();\n";
}

if (!$model->isNewRecord && $model->isMultiple()) {
    $js .= "$('.field-attribute-default_value').hide();\n";
}

$js .= "jQuery('.btn.multiple-input-list__btn.js-input-remove').off('click').on('click', function(){
    var itemId = $(this).closest('tr').data('id');
    if(itemId){
       $.ajax({url:'" . Url::toRoute('delete-subitem') . "',data:{id:itemId}}).done(function(result){;}).fail(function(result){alert(result);});
    }
});\n";

$js .= "jQuery(inputType).change(function(){
    if($.inArray($(this).val(), ['" . AttributeInputType::INPUT_TEXT . "', '" . AttributeInputType::INPUT_TEXTAREA . "', '" . AttributeInputType::INPUT_RICHTEXT . "']) !== -1){
        $('.field-attribute-default_value').show();
        $('#items-card').hide();
        // $('.field-attribute-items').hide();
    }
    else{
        $('.field-attribute-default_value').hide();
        $('#items-card').show();
        // $('.field-attribute-items').show();
    }
});\n";

// handle dependent input
if ($model->isNewRecord || !$model->is_depend) {
    $js .= "jQuery('.field-attribute-depend_id').hide();\n";
}
$js .= "jQuery(isDepend).change(function() {     
    $(dependId).parent().toggle();
});\n";
$this->registerJs($js);
?>